<template>
  <el-dialog v-model="visible" width="400px" title="设置" @opened="opened" @close="onClose">
    <template #header>
      <div class="my-header"> 设置 </div>
    </template>
    <el-form ref="formRef" :model="formParams" :rules="rules" label-width="100px">
      <el-form-item label="分享标题" prop="info">
        <el-input
          v-model="formParams.info"
          style="width: 300px"
          :autosize="{ minRows: 3, maxRows: 8 }"
          type="textarea"
          maxlength="30"
          placeholder="请输入内容"
          show-word-limit
        />
      </el-form-item>
      <el-form-item label="上传图片" prop="img">
        <ImageUploadByType
          v-model="formParams.img"
          :isShowTip="false"
          uploadType="1"
          :limit="1"
          :fileSize="10"
        />
        <div>图片的比例为5：4，如500px*400px</div>
      </el-form-item>
      <el-form-item label="社群选择" prop="shequn">
        <el-radio-group v-model="formParams.shequn">
          <el-radio value="1" label="1" size="large"> 全选 </el-radio>
          <br />
          <el-radio value="2" label="2" size="large">
            <el-select
              v-model="formParams.shequnValue"
              multiple
              filterable
              clearable
              fit-input-width
              collapse-tags
              collapse-tags-tooltip
              placeholder="请选择"
            >
              <el-option label="全部状态" value=""></el-option>
              <el-option label="已失效" value="0"></el-option>
              <el-option label="进行中" value="1"></el-option>
              <el-option label="待开始" value="2"></el-option>
            </el-select>
          </el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="handleAction(false)">取消</el-button>
      <el-button :loading="submit" type="primary" @click="handleAction(true)"> 确认 </el-button>
    </template>
  </el-dialog>
</template>

<script setup>
const emit = defineEmits(['close'])
const formRef = ref(null)
const visible = ref(false)
const submit = ref(false)
const formParams = reactive({
  info: '',
  img: '',
  shequn: '',
  shequnValue: ''
})
const checkShequn = (rule, value, callback) => {
  if ((value === '2' && !formParams.shequnValue?.length) || !value) {
    return callback(new Error(''))
  } else {
    callback()
  }
}
const rules = {
  shequn: [
    { required: true, message: '', trigger: 'change' },
    { validator: checkShequn, trigger: 'change' }
  ],
  img: [{ required: true, message: '请上传图片', trigger: 'change' }],
  info: [{ required: true, message: '', trigger: 'blur' }]
}
function handleAction(confirm) {
  if (confirm) {
    formRef.value.validate((valid) => {
      if (valid) {
        // submit.value = true
        // submit.value = false
      } else {
      }
    })
  } else {
    formRef.value.resetFields()
    visible.value = false
  }
}

function opened() {
  formRef.value.resetFields()
}

function onClose() {
  emit('close')
}

const init = () => {
  visible.value = true
}

defineExpose({
  init
})
</script>

<style lang="scss" scoped></style>
